import getLink from '@/api/getLink.js';

export const goods = {
  state: {
    PJArr: [],
    SzArr: [],
  },
  mutations: {
    SzArr(state, payload) {
      state.SzArr = payload;
    },
    PJArr(state, payload) {
      state.PJArr = payload;
    },

    //低到高
    lowHigh(state, type) {
      if (type == 'PJArr') {
        for (let i = 0; i < state.PJArr.length; i++) {
          for (let j = i + 1; j < state.PJArr.length; j++) {
            if (state.PJArr[i].price > state.PJArr[j].price) {
              [state.PJArr[i], state.PJArr[j]] = [state.PJArr[j], state.PJArr[i]];
            }
          }
        }
      } else {
        for (let i = 0; i < state.SzArr.length; i++) {
          for (let j = i + 1; j < state.SzArr.length; j++) {
            if (state.SzArr[i].price > state.SzArr[j].price) {
              [state.SzArr[i], state.SzArr[j]] = [state.SzArr[j], state.SzArr[i]];
            }
          }
        }
      }
    },

    //销量
    sale(state, type) {
      if (type == 'PJArr') {
        for (let i = 0; i < state.PJArr.length; i++) {
          for (let j = i + 1; j < state.PJArr.length; j++) {
            if (state.PJArr[i].number > state.PJArr[j].number) {
              [state.PJArr[i], state.PJArr[j]] = [state.PJArr[j], state.PJArr[i]];
            }
          }
        }
      } else {
        for (let i = 0; i < state.SzArr.length; i++) {
          for (let j = i + 1; j < state.SzArr.length; j++) {
            if (state.SzArr[i].number > state.SzArr[j].number) {
              [state.SzArr[i], state.SzArr[j]] = [state.SzArr[j], state.SzArr[i]];
            }
          }
        }
      }
    },

    //高到低
    highLow(state, type) {
      if (type == 'PJArr') {
        for (let i = 0; i < state.PJArr.length; i++) {
          for (let j = i + 1; j < state.PJArr.length; j++) {
            if (state.PJArr[i].price < state.PJArr[j].price) {
              [state.PJArr[i], state.PJArr[j]] = [state.PJArr[j], state.PJArr[i]];
            }
          }
        }
      } else {
        for (let i = 0; i < state.SzArr.length; i++) {
          for (let j = i + 1; j < state.SzArr.length; j++) {
            if (state.SzArr[i].price < state.SzArr[j].price) {
              [state.SzArr[i], state.SzArr[j]] = [state.SzArr[j], state.SzArr[i]];
            }
          }
        }
      }
    },
  },
  actions: {
    PJArr({ commit }) {
      getLink('/api/store/qt/3C').then(ok => commit('PJArr', ok.data));
    },
    SzArr({ commit }) {
      getLink('/api/store/qt/SZ').then(ok => commit('SzArr', ok.data));
    },
  },
};
